import random
import pygame

SCREEN_RECT = pygame.Rect(0, 0, 400, 600)
FRAME_PER_SEC = 60
CREATE_EMEMY_EVENT = pygame.USEREVENT
HERO_FIRE_EVENT = pygame.USEREVENT+1

class GameSprite(pygame.sprite.Sprite):

    def __init__(self, image_name, speed = 1):

        super().__init__()

        self.image = pygame.image.load(image_name)
        self.rect = self.image.get_rect()
        self.speed = speed


    def update(self):

        self.rect.y += self.speed


class BackGround(GameSprite):

    def __init__(self, is_alt=False):

        super().__init__("./images/background.png")
        if is_alt:
            self.rect.y = -self.rect.height

    def update(self):

        super().update()

        if self.rect.y > SCREEN_RECT.height:

            self.rect.y = -self.rect.height


class Eneny(GameSprite):

    def __init__(self):

        super().__init__("./images/enemy1.png")
        self.speed = random.randint(1, 4)
        self.rect.bottom = 0
        Max = SCREEN_RECT.width - self.rect.width

        self.rect.x = random.randint(0, Max)


    def update(self):

        super().update()

        if self.rect.y >= SCREEN_RECT.height:

            # print("飞出屏幕，需要删除...")
            self.kill()

    def __del__(self):

        # print("敌人的飞机挂了%s " %self.rect)
        pass


class Hero(GameSprite):

    def __init__(self):

        super().__init__("./images/me1.png", 0)

        self.rect.centerx = SCREEN_RECT.centerx
        self.rect.y = SCREEN_RECT.bottom - 250

        self.bullet_gruop = pygame.sprite.Group()

    def update(self):
        self.rect.x += self.speed

        if self.rect.x<0:

            self.rect.x = 0

        elif self.rect.right > SCREEN_RECT.right:

            self.rect.right = SCREEN_RECT.right

    def fire(self):

        for i in (0, 1, 2):
            bullet = Bullet()

            bullet.rect.bottom = self.rect.y - i * 20
            bullet.rect.centerx = self.rect.centerx

            self.bullet_gruop.add(bullet)


class Bullet(GameSprite):

    def __init__(self):

        super().__init__("./images/bullet1.png", -2)


    def update(self):

        super().update()

        if self.rect.bottom < 0:

            self.kill()

    def __del__(self):

        print("子弹销毁")